/**
 *
 * 2007-11-2 下午06:48:12
 * @author xuhuan
 */

package edu.ccut.saturn.studio.logic.model.xml.loader;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnBaseComponentNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnComponentNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnParameter;

public class ComponentNodeLoader extends NodeLoader {

	public List<Shape> loadNodes(Element root) {

		NodeList componentNodes = root
				.getElementsByTagName(SaturnComponentNode.KEY_NODE_COMPONENT);
		List<Shape> shapes = new ArrayList<Shape>(componentNodes.getLength());
		for (int i = 0; i < componentNodes.getLength(); ++i) {
			Node componentNode = componentNodes.item(i);
			SaturnBaseComponentNode saturnComponentNode = ComponentFactory.getSaturnBaseComponentNode(super.getConfigInfo(componentNode, "type"));
			
			String shapeId = super.getNodeAttributeValue(componentNode,
					Shape.KEY_ATTRIBUTE_SHAPE_ID);
			String nextId = super.getNodeAttributeValue(componentNode,
					Shape.KEY_ATTRIBUTE_SHAPE_NEXT_ID);
			
			
			
			String exception = super.getNodeAttributeValue(componentNode,
					SaturnComponentNode.SHOW_COMPONENT_NODE_EXCEPTION);
			
			String key = super.getNodeElementValue(componentNode,
					SaturnComponentNode.SHOW_COMPONENT_NODE_KEY);
			String name = super.getNodeElementValue(componentNode,
					SaturnComponentNode.SHOW_COMPONENT_NODE_NAME);
			String description = super.getNodeElementValue(componentNode,
					SaturnComponentNode.SHOW_COMPONENT_NODE_DESCRITPION);
			List<SaturnParameter> parameters = super
					.getParameters(componentNode);

			Point location = super.getLocation(componentNode);
			Dimension size = new Dimension(50, 70);

			saturnComponentNode.setId(shapeId);
			saturnComponentNode.setNode(componentNode);
			saturnComponentNode.setLocation(location);
			saturnComponentNode.setKey(key);
			saturnComponentNode.setName(name);
			saturnComponentNode.setNextId(nextId);
			saturnComponentNode.setDescription(description);
			saturnComponentNode.setParameters(parameters);
			saturnComponentNode.setSize(size);
			saturnComponentNode.setException(exception); 
			saturnComponentNode.setFullKey(super.getConfigInfo(componentNode, "fullKey"));
			shapes.add(saturnComponentNode);
		}

		return shapes;
	}
}
